/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.publisher.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alien.admin.modules.deploy.domain.DeployRec;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Set;

/**
* @website https://el-admin.vip
* @description /
* @author zhouseyi
* @date 2021-11-11
**/
@Entity
@Data
@Table(name="dp_publish_deploy")
public class PublishDeploy implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    @ApiModelProperty(value = "主键")
    private Integer id;

    @Column(name = "flow_id",nullable = false)
    @NotNull
    @ApiModelProperty(value = "流程id")
    private Long flowId;

    @Column(name = "flow_num")
    @ApiModelProperty(value = "流程部署次数")
    private Long flowNum;

    @Column(name = "env",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "环境")
    private String env;

    @Column(name = "publish_time",nullable = false)
    @NotNull
    @ApiModelProperty(value = "部署时间")
    private Timestamp publishTime;

    @Column(name = "stage",nullable = false)
    @NotNull
    @ApiModelProperty(value = "阶段：0:合并分支 1:服务部署 2:分支回滚 3.服务回滚 4.结束")
    private Integer stage;

    @Column(name = "status",nullable = false)
    @NotNull
    @ApiModelProperty(value = "状态：0:进行中 1:回滚 2:成功 3.失败")
    private Integer status;

    @Column(name = "operator_id",nullable = false)
    @NotNull
    @ApiModelProperty(value = "部署人id")
    private Long operatorId;

    @Column(name = "operator_name",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "部署人姓名")
    private String operatorName;

    @OneToMany(cascade={CascadeType.PERSIST,CascadeType.REMOVE}, fetch = FetchType.EAGER)
    @JoinColumn(name = "publish_deploy_id", updatable = false, insertable = false)
    private Set<DeployRec> deployRecs;

    public void copy(PublishDeploy source){
        BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
    }
}